﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//Author: ljj
//Date:20190701
//Function:管理特效obj,实现重复利用,减少实例化次数
public class EffectObjectManager : MonoBehaviour
{

    public static EffectObjectManager Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = FindObjectOfType<EffectObjectManager>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("APController");
                _Instance = obj.AddComponent<EffectObjectManager>();
            }
            return _Instance;
        }
    }
    private static EffectObjectManager _Instance;

    Dictionary<string, List<GameObject>> effectObjectMap = new Dictionary<string, List<GameObject>>();

    static Vector3 invalidPos = new Vector3(-10, -10, 0);




    public GameObject CreateEffectObject(GameObject prefab, Vector3 pos, Quaternion rot)
    {
        List<GameObject> list;
        if (!effectObjectMap.TryGetValue(prefab.name, out list)) 
        {
            list = new List<GameObject>();
            effectObjectMap.Add(prefab.name, list);
        }
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i] != null && list[i].transform.position == invalidPos)
            {
                list[i].transform.position = pos;
                list[i].transform.rotation = rot;
                InitEffectObject(list[i]);
                return list[i];
            }
        }
        GameObject obj = GameObject.Instantiate(prefab, pos, rot, transform);
        obj.layer = LayerMask.NameToLayer("Default");

        list.Add(obj);
        InitEffectObject(obj);
        return obj;
    }

    public GameObject CreateEffectObject(GameObject prefab, Vector3 pos, Quaternion rot,Transform parent)
    {
        List<GameObject> list;
        if (!effectObjectMap.TryGetValue(prefab.name, out list))
        {
            list = new List<GameObject>();
            effectObjectMap.Add(prefab.name, list);
        }
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i] != null && list[i].transform.position == invalidPos)
            {
                list[i].transform.position = pos;
                list[i].transform.rotation = rot;
                InitEffectObject(list[i]);
                return list[i];
            }
        }
        GameObject obj = GameObject.Instantiate(prefab, pos, rot, parent);
        obj.layer = LayerMask.NameToLayer("Default");

        list.Add(obj);
        InitEffectObject(obj);
        return obj;
    }

    public GameObject CreateEffectObject(string path, Vector3 pos)
    {
        GameObject obj = Resources.Load(path) as GameObject;
        return CreateEffectObject(obj, pos, Quaternion.identity);
    }

    void InitEffectObject(GameObject obj)
    {
        obj.SetActive(true);
        Animator[] animators = obj.GetComponentsInChildren<Animator>();
        for (int i = 0; i < animators.Length; i++)
        {
            string first_animationName = animators[i].GetCurrentAnimatorClipInfo(0)[0].clip.name;
            animators[i].Play(first_animationName);
            animators[i].Update(0);
        }

        ParticleSystem[] particles = obj.GetComponentsInChildren<ParticleSystem>();
        for (int i = 0; i < particles.Length; i++)
        {
            particles[i].Stop();
            particles[i].Play();
        }

        DestroyTimer[] timers = obj.GetComponentsInChildren<DestroyTimer>();
        for (int i = 0; i < timers.Length; i++)
        {
            timers[i].Init();
        }
    }
    public void ButtonEffects()
    {
        CreateEffectObject("Prefabs/Effect/ButtonSound", Vector3.zero);
    }
    public void TowerBuildEffects()
    {
        CreateEffectObject("Prefabs/Effect/TowerBuildSound", Vector3.zero);
    }
    public void TowerSellEffects()
    {
        CreateEffectObject("Prefabs/Effect/TowerSellSound", Vector3.zero);
    }
    public void TowerUpgradeEf0fects()
    {
        CreateEffectObject("Prefabs/Effect/TowerUpgradeSound", Vector3.zero);
    }



    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
